import {
  statisticsReq,
  statisticsListItem,
  userListItem,
  wordFreqRes,
  questionListItem,
  pageReq,
  zhaoshengDashboardDataRes,
  hisReq
} from '@/common/types';
import apiClient from '../apiClient';

export enum ReportApi {
  StatisticsAllApi = '/openapi/v1/statistics',
  StatisticsListApi = '/openapi/v1/statistics/app',
  WordFreqApi = '/openapi/v1/statistics/wordcloud',

  StatisticsAllApiV2 = '/openapi/v2/zhaosheng/statistics',
  StatisticsListApiV2 = '/openapi/v2/zhaosheng/statistics/app',
  WordFreqApiV2 = '/openapi/v2/zhaosheng/statistics/wordcloud',

  // AppUserListApi = '/openapi/v1/app/{app_id}/users',
  AppUserListApi = '/openapi/v2/statistics/{app_id}/users',
  // AppUserListApi = '/openapi/v1/app/{app_id}/userlist',
  // QuestionUserListAPi = '/openapi/v1/app/{app_id}/{user_id}/questions',
  QuestionUserListAPi = '/openapi/v2/statistics/{app_id}/{user_id}/questions',
  QAListAPi = '/openapi/v1/app/qa',


  ExportChatsApi = '/openapi/v1/shedule/task/export_chats',
  ExportUnknownApi = '/openapi/v1/shedule/task/unknown',

  ZhaoshengDashboardActive = "/openapi/v2/zhaosheng/dashboard/active-hours",
  ZhaoshengDashboardData = "/openapi/v2/zhaosheng/dashboard/data",
  ZhaoshengHistoryQuestions = "/openapi/v2/zhaosheng/history/questions",

  //应用看板
  statisticsTrendsApi = '/openapi/v2/statistics',
}
const statisticsTrends = (appId: string, data: hisReq) => apiClient.get({ url: ReportApi.statisticsTrendsApi + `/${appId}/userDigital`, params: data });
const statisticsDialogue = (appId: string, data: hisReq, urlText: string) => apiClient.get({ url: ReportApi.statisticsTrendsApi + `/${appId}/${urlText}`, params: data });
const statisticsTop = (appId: string, data: hisReq) => apiClient.get({ url: ReportApi.statisticsTrendsApi + `/${appId}/top/question`, params: data });
const getStatisticsData = (appId: string) => apiClient.get({ url: ReportApi.statisticsTrendsApi + `/${appId}` });


// const statisticsAll = (id: string) => apiClient.get<statisticsListItem>({ url: ReportApi.StatisticsAllApi + `/${id}`});
const statisticsAll = (id: string, role_id?: string) => {
  let url = ReportApi.StatisticsAllApi + `/${id}`;

  if (role_id) {
    url += `?role=${role_id}`;
  }

  return apiClient.get<statisticsListItem>({ url });
};

const statisticsAllV2 = (id: string, role_id?: string) => {
  let url = ReportApi.StatisticsAllApiV2 + `/${id}`;

  if (role_id) {
    url += `?role=${role_id}`;
  }

  return apiClient.get<statisticsListItem>({ url });
};


// const statisticsList = (id: string, data: statisticsReq) => apiClient.get<statisticsListItem[]>({ url: ReportApi.StatisticsListApi + `/${id}`, params: data });
const statisticsList = (id: string, data: statisticsReq, role_id?: string,) => {
  let url = ReportApi.StatisticsListApi + `/${id}`;
  let params = data;

  if (role_id) {
    url = url + `?role=${role_id}`;
  }

  return apiClient.get<statisticsListItem[]>({ url, params });
};

const statisticsListV2 = (id: string, data: statisticsReq, role_id?: string,) => {
  let url = ReportApi.StatisticsListApiV2 + `/${id}`;
  let params = data;

  if (role_id) {
    url = url + `?role=${role_id}`;
  }

  return apiClient.get<statisticsListItem[]>({ url, params });
};


// const appUserList = (app_id: string) => apiClient.get<userListItem[]>({ url: `${ReportApi.AppUserListApi.replace('{app_id}', app_id)}` });


const questionUserList = (app_id: string, userId: string, data?: pageReq, signal?: AbortSignal) =>
  apiClient.get<userListItem>({
    url: ReportApi.QuestionUserListAPi.replace('{app_id}', app_id).replace('{user_id}', userId),
    params: data,
    signal,
  });
const qAList = (chat_id: string, startTime?: string, endTime?: string, page: number = 1, pageSize: number = 10, offset: number = 0) =>
  apiClient.get<questionListItem[]>(
    { url: `${ReportApi.QAListAPi}/${chat_id}?startTime=${startTime}&endTime=${endTime}&page=${page}&page_size=${pageSize}&offset=${offset}` }
  );

// const wordFreq = (app_id: string) => apiClient.get<wordFreqRes>({ url: `${ReportApi.WordFreqApi}/${app_id}` });
const wordFreq = (app_id: string, role_id?: string) => {
  let url = `${ReportApi.WordFreqApi}/${app_id}`;

  if (role_id) {
    url += `?role=${role_id}`;
  }

  return apiClient.get<wordFreqRes>({ url });
};

const wordFreqV2 = (app_id: string, role_id?: string) => {
  let url = `${ReportApi.WordFreqApiV2}/${app_id}`;

  if (role_id) {
    url += `?role=${role_id}`;
  }

  return apiClient.get<wordFreqRes>({ url });
};

const appUserList = (app_id: string, role_id?: string, page: number = 1, page_size: number = 100, name: string = "", rangePickerValue = []) => {
  let url = `${ReportApi.AppUserListApi.replace('{app_id}', app_id)}`;

  url += `?page=${page}&page_size=${page_size}`;

  if (role_id) {
    url += `&role=${role_id}`;
  }
  if (name) {
    url += `&name=${name}`;
  }

  if (rangePickerValue.length == 2) {
    url += `&startTime=${rangePickerValue[0]}&endTime=${rangePickerValue[1]}`;
  }


  return apiClient.get<userListItem[]>({ url });
};


const exportQAData = (app_id?: string, rangePickerValue: string[] = [], username = "") => {
  let url: string = ReportApi.ExportChatsApi.toString();

  if (app_id) {
    url += `?appId=${app_id}`;
  }

  if (username) {
    url += `${app_id ? '&' : '?'}name=${username}`;
  }

  if (rangePickerValue.length == 2) {
    url += `${app_id || username ? '&' : '?'}startTime=${rangePickerValue[0]}&endTime=${rangePickerValue[1]}`;
  }

  return apiClient.get({ url });
}


const exportUnknownData = (app_id?: string, rangePickerValue: string[] = [], signal?: AbortSignal) => {
  let url = ReportApi.ExportUnknownApi.toString();

  if (app_id) {
    url += `?appId=${app_id}`;
  }

  if (rangePickerValue.length == 2) {
    url += `${app_id ? '&' : '?'}startTime=${rangePickerValue[0]}&endTime=${rangePickerValue[1]}`;
  }

  return apiClient.get({ url, signal });
}


const zhaoshengDashboardActive = (app_id: string, data: statisticsReq) => {
  let url = `${ReportApi.ZhaoshengDashboardActive}/${app_id}`;
  let params = data;
  return apiClient.get<wordFreqRes>({ url, params });
};

const zhaoshengDashboardData = (app_id: string, data: statisticsReq) => {
  let url = `${ReportApi.ZhaoshengDashboardData}/${app_id}`;
  let params = data;
  return apiClient.get<zhaoshengDashboardDataRes>({ url, params });
};

const zhaoshengHistoryQuestions = (app_id: string, data: hisReq) => {
  let url = `${ReportApi.ZhaoshengHistoryQuestions}/${app_id}`;
  let params = data;
  return apiClient.post<zhaoshengDashboardDataRes>({ url, data: params });
};




export default {
  statisticsTrends,
  statisticsDialogue,
  statisticsTop,
  getStatisticsData,

  statisticsAll,
  statisticsList,
  appUserList,
  questionUserList,
  qAList,
  wordFreq,
  zhaoshengDashboardActive,
  zhaoshengDashboardData,
  zhaoshengHistoryQuestions,

  exportQAData,
  exportUnknownData,
  statisticsAllV2,
  statisticsListV2,
  wordFreqV2
};
